<?php

require_once(dirname(__FILE__).'/../products/aimersoft_products.php');

class AimersoftBuy extends AimersoftProducts {
	var $db;
	function __construct(){
		$this->db = $GLOBALS['db'];
		parent::__construct( $this->db );
	}
	
	//note 产品价格
	function productPrice( $get ){
		$str = parent::productPrice( $get );
		return $str;
	}
	
	//note 产品购买链接
	function productBuyLink( $get ){
		$id = $get['产品ID'];
		$price = PublicProducts::productPrice( $id );
		return $price['swreg_url'];
	}
	

	/**
	 * 产品页面地址
	 * */
	function bindingRelatedProduct( $get ){
		
		$id = $get["产品ID"];
		$sql="SELECT p.product_id, p.product_box_min, p.product_cbsid, tab.url, won_p.id 
			FROM product  p 
			LEFT JOIN won_product won_p 
			ON p.product_id = won_p.won_product_id   
			LEFT JOIN Table_174  tab  
			ON tab.Field_21895 = won_p.id 
			WHERE won_p.id  = {$id} AND won_p.won_product_delete = 0  AND p.product_delete = 0 "; 
		$rows = $this->db->getOneRs($sql);
		if (empty($rows)) return '';
		
		$product_id = $rows['product_id'];
		
		$product_bind_data = json_decode(file_get_contents('http://cbs.wondershare.cn/interface.php?m=recommend'),true);
		$data_cbs = array();
		$data_bind =  $product_bind_data[$rows['product_cbsid']];
		
		if (!empty($data_bind)) {
			foreach ($data_bind  AS $keys=>$values) {
				
				$binds = explode('_',$keys);
				$p_bind_cbsid .= empty($p_bind_cbsid ) ? $binds[0] : ','.$binds[0];
				$data_cbs[$binds[0]] = array('price' => $values, 'sub_license_id' => $binds[2] );
			}
		} else {
			 return '';
		}
		
		$p_bind_cbsid = empty($p_bind_cbsid) ? $rows['product_cbsid'] : $rows['product_cbsid'].','.$p_bind_cbsid;
		
		$sql = "SELECT  product_intro,product_id,product_name,product_box_min,
				 product_brand,product_is_year_charge,product_cbsid  
				 FROM  product  
				 WHERE FIND_IN_SET(product_cbsid, '{$p_bind_cbsid}')  AND product_delete = 0
				 ORDER BY  FIND_IN_SET(product_cbsid, '{$p_bind_cbsid}') LIMIT 3";
		$result = $this->db->query($sql);
		
		$main_new_price = $main_old_price =  0;
		$nums = 0;
		$start_swreg_v_value = $start_swreg_p_value = $main_product_url = '';
		
		while ($row = $this->db->fetch_array($result)) {
			
			
			$sql = "SELECT tab.url FROM won_product won_p  
					LEFT JOIN Table_174 tab  
					ON tab.Field_21895 = won_p.id 
					WHERE won_p.won_product_id =  '{$row['product_id']}' ";
			$product_url = $this->db->getOne($sql);
			if (empty($product_url)) {
				
				$sql = "SELECT tab.url FROM won_product won_p  
						LEFT JOIN Table_175 tab  
						ON Field_21901 = won_p.id  
						WHERE won_p.won_product_id =  '{$row['product_id']}' ";
				$product_url= $this->db->getOne($sql);
			}
			
			$product_url = str_replace(array('index.html','/aimersoft.com/httpdocs/')  , array('', 'http://www.aimersoft.com/'), $product_url); 
			
			$year_str = '';
			if ($row['product_is_year_charge'] == 1) {
				$year_str= '/Year';
			}
			
		    $sql = "SELECT license_id,sub_license_id,price,website_display, swreg_url
		    		FROM license WHERE product_id = '{$row['product_id']}'  AND license_id = 11 AND license_delete = 0 AND sub_license_id = 0 ";	
		    $license_rows = $this->db->getOneRs($sql);
		
			$is_user_license_id = $row['product_id'] == $product_id ? 0 : 1;
			$price_one = get_price_info($row['product_id'], (int) $data_cbs[$row['product_cbsid']]['sub_license_id'], $is_user_license_id);
			$old_price = $price_one['old'];
			
		    if	($row['product_id'] == $product_id) {
			    $new_price = $price_one['curr'];
		    } else  {
		    	$new_price = $data_cbs[$row['product_cbsid']]['price']['USD'];
		    }
		    
			 $old_str = $new_price != $old_price ? '<s>'.$price_one['old'].$year_str.'</s>' : '';
			
		    $url_arr = parse_url($price_one['swreg_url']);
		    $v_value = $p_value = '';
		    foreach (explode('&', $url_arr['query']) AS $key => $value) {
		    	
		    	$value_arr  = explode('=', trim($value));
		    	if (strtolower($value_arr[0]) == 'v') {
		    		$v_value = $value_arr[1];
		    	} else if (strtolower($value_arr[0]) == 'p') {
		    		$p_value = $value_arr[1];
		    	}
		    }
		    $start_swreg_v_value .= $nums == 0 ?  $v_value : ':'.$v_value;
		    $start_swreg_p_value .= $nums == 0 ?  $p_value : ':'.$p_value;
		    
			$old_sum_price += $old_price;
			$new_sum_price += $new_price;
			
			if ($row['product_id'] != $product_id) {
		        
			   	 $str .= '<span class="cross"></span><div id="recommend_product-1" class="item">
				         <div class="select">
				                <label>
				                  <input name="product_check[]"   type="checkbox" value="'.$row['product_cbsid'].'" 
				                   new_price="'.$new_price.'"  old_price="'.$old_price.'" 
				                   p_value="'.$p_value.'"  v_value="'.$v_value.'"   checked="checked" />
				                  '.trim(str_replace($row['product_brand'],'',$row['product_name'])).'</label>
				              </div>
				              <div class="proInfo oh"> <span class="img"> <a href="'.$product_url.'" target="_blank">
				              <img src="'.$row['product_box_min'].'" width="80" height="80" /></a></span>
				              <div class="descrip">'.$row['product_intro'].'<div><a href="'.$product_url.'">Learn More &gt;</a></div></div>
				               <div class="price"> <span><strong>$'.$new_price.$year_str.'</strong></span>$'.$old_str.'</div>
				             </div>
				            </div>';
			    
			} else {
				
		      	$main_swreg_hidden = '<input type="hidden" id="main_swreg" p_value="'.$p_value.'" v_value="'.$v_value.'" />';
				$main_product_url = $product_url;
				$main_new_price = $new_price;
				$main_old_price = $old_price;
			}
			$nums = $nums + 1;
		    
		}
		
		//在原产品下面加价格
		$main_old_price_str = '';
		if ($main_new_price != $main_old_price ) {
			$main_old_price_str = '$<del>'.$main_old_price.'</del>';
		}
		
		$str = '<td><div class="fl mt33"><a href="'.$p_url.'" target="_blank">
		<img src="'.$rows['product_box_min'].'" width="80" height="80"/></a>
		<p class="s_price">$'.$main_new_price.$main_old_price_str.'</p>
		</div>'.$str;
		
		if ($nums == 2 ) {
		    $show_text = 'for Both';
		} else if ($nums == 3) {
			$show_text = 'for All Three';
		}
		
		$start_swreg_url = 'https://usd.swreg.org/cgi-bin/s.cgi?s=46347&q=1&d=0&p='.$start_swreg_p_value.'&v='.$start_swreg_v_value;
		
		$str .= '<td ><input type="hidden" id="start_swreg" value="https://usd.swreg.org/cgi-bin/s.cgi?s=46347&q=1&d=0" />'.$main_swreg_hidden.'
				<input type="hidden" id="main_price" value="" new_price="'.$main_new_price.'"  old_price="'.$main_old_price.'"  />
				<div class="pay"> 
				<span>Price <span id="show_text">'.$show_text.'</span></span>
				<span class="now"><span id="old_sum">'.$new_sum_price.'</span></span>
				 <span class="save">Save $<span id="save_sum">'.($old_sum_price - $new_sum_price).'</span></span>
				<div>
				<a href="'.$start_swreg_url.'" class="buyBtn" id="buy_url" style="display: block; "
				 onclick="_gaq.push([\'_trackEvent\', \'Bundle\', \'Buy\', document.location.pathname +\'<>\']);_gaq.push([\'_link\', this.href]); return false;" >
				</a></div>
				</div></td>';
		
		if ($nums == 0) {
		 	return '';
		} else {
		    return '<div class="special">
		    <div class="line"></div>
		    <h2>Special Offers Featuring '.$get["产品名称"].', Save Up To 
		    <span class="m_price">$'.($old_sum_price - $new_sum_price).'</span>
		    </h2>
		    <div class="specialArea"><table width="" border="0">
		    <tr>
		    '.$str.'</tr></table></div></div>';
		 }
	}
	
	

}
?>